<?php

namespace App\Console\Commands;

use App\Model\Spread;
use Illuminate\Console\Command;

/**
 * 脉宝分享自动下架，清楚redis数据
 * Class SpreadTask
 * @package App\Console\Commands
 */
class SpreadTask extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:spreadTask';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $spread = new Spread();
        while(1){
            $data = $spread
                ->where("maibao_num_surplus", '=',0)
                ->where("is_deleted", "=", 0)
                ->where("status",'=',2)
                ->pluck("id");

            if (count($data) > 0 ){
                echo "正在执行！";
                $spread->whereIn("id",$data)->update(["status"=>3]);
                try{
                    \DB::beginTransaction();
                    $res =  $spread->whereIn("id",$data)->update(["status"=>3]);
                    if ($res < 0){
                        \DB::rollBack();
                        throw  new  \Exception("修改失败");
                    }
                    \DB::commit();
                }catch (\Exception $e ){
                    \Log::error($e);
                }
            }else{
                echo "10后在执行！";
                sleep(10);
            }
        }
    }
}
